﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Alogient.Cameleon.Sdk.UI.WebControls
{
    [DefaultProperty("Value")]
    [ToolboxData("<{0}:CMImage runat=server></{0}:CMImage>")]
    public class CMImage : Image
    {
        protected int navID = NavigationController.GetNavID();
        protected string cultureCode = NavigationController.GetCultureCode();
        protected string alternateText = null;
        protected string originalImageFile = null;
        protected string presentationImageFile = null;
        protected string thumbnailImageFile = null;
        
        [Category("Cameleon")]
        [Browsable(true)]
        public string BindField { get; set; }

        [Category("Cameleon")]
        [Browsable(true)]
        public ImageFileType ImageFileType { get; set; }

        public CMImage()
        {
            this.ImageFileType = ImageFileType.Original;
        }

        public string CMAlternateText
        {
            get
            {
                if (this.alternateText == null && this.BindField != null)
                {
                    this.alternateText = CMServices.GetPage(this.navID, this.cultureCode).Elements[this.BindField].Value;
                }

                return this.alternateText;
            }
        }

        public string OriginalImageFile
        {
            get
            {
                if (this.originalImageFile == null && this.BindField != null)
                {
                    this.originalImageFile = CMServices.GetPage(this.navID, this.cultureCode).Elements[this.BindField].OriginalMediaFile;
                }

                return this.originalImageFile;
            }
        }

        public string PresentationImageFile
        {
            get
            {
                if (this.presentationImageFile == null && this.BindField != null)
                {
                    this.presentationImageFile = CMServices.GetPage(this.navID, this.cultureCode).Elements[this.BindField].PresentationImageFile;
                }

                return this.presentationImageFile;
            }
        }

        public string ThumbnailImageFile
        {
            get
            {
                if (this.thumbnailImageFile == null && this.BindField != null)
                {
                    this.thumbnailImageFile = CMServices.GetPage(this.navID, this.cultureCode).Elements[this.BindField].ThumbnailImageFile;
                }

                return this.thumbnailImageFile;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            this.AlternateText = this.CMAlternateText;

            switch (this.ImageFileType)
            {
                default: break;
                case ImageFileType.Original:
                    {
                        this.ImageUrl = this.OriginalImageFile;
                    } 
                    
                    break;
                case ImageFileType.Presentation:
                    {
                        this.ImageUrl = this.PresentationImageFile;
                    } 
                    
                    break;
                case ImageFileType.Thumbnail:
                    {
                        this.ImageUrl = this.ThumbnailImageFile;
                    } 
                    
                    break;
            }

            base.Render(writer);
        }
    }
}
